<?php
class ExchangeController extends BaseController
{   
    public function _initialize() {
        parent::_initialize();
        $this->id = session('id');
    }
    public function index()
    {  
        //奖池总金额 
        $Jackpot = M('system_config')->where(['id'=>1])->find();
        //中奖记录
        $records  = M('upvaluation_put_records up')->where(['up.if_reward'=>array('neq',1)])->join('__USER__ u on u.id=up.user_id')->field('u.username,up.*')->select();

        $user_info = M('user')->where(['id'=>$this->id])->find();
        // var_dump($user_info);die;
        //投注总金额 
        $touzhu_balance = M('system_config')->where(['id'=>3])->find()['value'];

        //计算价格
        $beans_balance = (int)floor($touzhu_balance/1000);
        $beans_balance = $beans_balance ? round(1/((100 - 0.002*$beans_balance)/100),6) : 1;

        //当前投注，投注总计

        include template('/user/exchange/index');
    }
    //GF投注
    public function buy()
    {
        if(IS_POST){
            $gf = $_POST['gf'];
            $beans = $_POST['beans'];
            $user_info = M('user')->where(['id'=>$this->id])->find();

            //中奖
            $gailv = M('system_config')->where(['id'=>4])->find()['value'];
            $gailv = $gf < 100 ? $gailv : $gailv +  5;
            $rand = mt_rand(1,1000);
           
            //新投注前的最后一条记录）
            $have_touzhu_records = M('upvaluation_put_records')->order('id desc')->limit(1)->find();
            try{
                M()->startTrans();
                //个人信息变动
                $save = [
                    'upvaluation'=>(($user_info['upvaluation'] - $gf) > 0 ) ? ($user_info['upvaluation'] - $gf) : 0,
                    'beans'=> $user_info['beans'] + $beans,
                    'put_upvaluation'=>$user_info['put_upvaluation'] + $gf,
                ];

                $change_upvaluation_records = [
                    'user_id'=>$this->id,
                    'balance'=>-$gf,
                    'type'=>8,
                    'add_time'=>date('Y-m-d H:i:s'),
                    'remarks'=>'投注扣除GF'
                ];
                M('upvaluation_exchange_records')->add($change_upvaluation_records);
                // var_dump($save);die;
                M('user')->where(['id'=>$this->id])->save($save);

                //奖池金额变动（增加）
                M('system_config')->where(['id'=>1])->setInc('value',$gf*0.15);

                //投注总金额变动
                M('system_config')->where(['id'=>3])->setInc('value',$gf);

                //团队手续费变动
                M('system_config')->where(['id'=>2])->setInc('value',$gf*0.03);
 
                //中奖
                $records = [
                    'user_id'=>$this->id,
                    'balance'=>$gf,
                    'beans'=>$beans,
                    'add_time'=>date('Y-m-d H:i:s')
                ];

                if($rand <= $gailv){
                   
                    $jiangchi = M('system_config')->where(['id'=>1])->find();
                    //中奖
                    if($gf >= 100 && $gf < 500){
                        $reward_prize = ($jiangchi['value'] < 200) ? $jiangchi : 200;
                    }elseif($gf >= 500 && $gf < 1000){
                        $reward_prize = ($jiangchi['value'] < 1000) ? $jiangchi : 1000;
                    }elseif($gf >= 100){
                        $reward_prize = ($jiangchi['value'] < 2000) ? $jiangchi : 2000;
                    }elseif($gf < 100){
                        $reward_prize = 0;
                    }
                    $records['reward_prize'] = $reward_prize;
                    $records['if_reward'] = 2;
                    //中奖概率重置
                    M('system_config')->where(['id'=>4])->setField('value',0);
                    //中奖之后奖池金额变动
                    M('system_config')->where(['id'=>1])->setDec('value',$reward_prize);
                    //中奖记录
                    //个人信息系变动
                    M('user')->where(['id'=>$this->id])->setInc('upvaluation',$reward_prize);
                    M('upvaluation_put_records')->add($records);
                    $zhongjiang_records = [
                        'user_id'=>$this->id,
                        'balance'=>$reward_prize,
                        'type'=>5,
                        'add_time'=>date('Y-m-d H:i:s'),
                        'remarks'=>'投注中奖'
                    ];
                    M('upvaluation_exchange_records')->add($zhongjiang_records);
                }else{
                    //未中奖
                
                    $records['reward_prize'] = 0;
                    $records['if_reward'] = 1;
                    M('system_config')->where(['id'=>4])->setField('value',$gailv);
                    M('upvaluation_put_records')->add($records);
                }

                //分红
                //判定是否出局
                $fenhong_info = M('user')->where(['id'=>$have_touzhu_records['user_id']])->find();
                if($fenhong_info['red_upvaluation'] < 2*$fenhong_info['put_upvaluation']){
            
                    //可以分红
                    $all_num = M('user')->sum('beans');
                    $fenhoong_money = ($all_num == 0) ? 0 : ($fenhong_info['beans']/$all_num)*$gf*0.7;
                    // $fenhoong_money = ($fenhong_info['beans']/10000)*$gf*0.7;
                   
                    $fenhoong_money = ($fenhoong_money + $fenhong_info['red_upvaluation'] >= $fenhong_info['put_upvaluation']) ? 2*$fenhong_info['put_upvaluation'] - $fenhong_info['red_upvaluation'] : $fenhoong_money;
                    $fenhong_add = [
                        'red_upvaluation'=>$fenhong_info['red_upvaluation'] + $fenhoong_money,
                        'upvaluation'=>$fenhong_info['upvaluation'] + $fenhoong_money,
                    ];
                    M('user')->where(['id'=>$fenhong_info['id']])->save($fenhong_add);
                    //分红记录
                    if($fenhoong_money != 0){
                        $fenhong_records =[
                            'user_id'=>$this->id,
                            'user_mobile'=>$user_info['username'],
                            'get_profit_id'=>$fenhong_info['id'],
                            'get_profit_mobile'=>$fenhong_info['username'],
                            'balance'=>$fenhoong_money,
                            'type'=>1,
                            'remarks'=>'获取'.$user_info['username'].'升值链分红'.$fenhoong_money,
                            'add_time'=>date('Y-m-d H:i:s')
                        ];
                        M('fenhong_records')->add($fenhong_records);


                        $upvaluation_records =[
                            'user_id'=>$fenhong_info['id'],
                            'balance'=>$fenhoong_money,
                            'type'=>4,
                            'add_time'=>date('Y-m-d H:i:s'),
                            'remarks'=>'投注分红获利'
                        ];
                        M('upvaluation_exchange_records')->add($zhongjiang_records);
                    }
                }
               
                //邀请奖励
                //直接邀请人
               
                M('user')->where(['id'=>$user_info['refer_id']])->setInc('upvaluation',0.1*$gf);
                M('user')->where(['id'=>$user_info['refer_id']])->setInc('invite_upvaluation',0.1*$gf);
                
                $oen = [
                    'user_id'=>$user_info['refer_id'],
                    'balance'=>0.1*$gf,
                    'type'=>6,
                    'add_time'=>date('Y-m-d H:i:s'),
                    'remarks'=>'作为投注人上级获利'
                ];

                M('upvaluation_exchange_records')->add($oen);
                
                //上上级
                $refer_info = M('user')->where(['id'=>$user_info['refer_id']])->field('id,refer_id')->find();
                if($refer_info['refer_id'] != 0){
                   M('user')->where(['id'=>$refer_info['refer_id']])->setInc('upvaluation',0.02*$gf);
                   M('user')->where(['id'=>$refer_info['refer_id']])->setInc('invite_upvaluation',0.02*$gf);
                   $two = [
                    'user_id'=>$refer_info['refer_id'],
                    'balance'=>0.02*$gf,
                    'type'=>6,
                    'add_time'=>date('Y-m-d H:i:s'),
                    'remarks'=>'作为投注人上级获利'
                ];

                M('upvaluation_exchange_records')->add($two);
                    //上上上级
                    $two_refer_info = M('user')->where(['id'=>$refer_info['refer_id']])->field('id,refer_id')->find();
                    if($two_refer_info['refer_id'] != 0){
                        M('user')->where(['id'=>$two_refer_info['refer_id']])->setInc('upvaluation',0.02*$gf);
                        M('user')->where(['id'=>$two_refer_info['refer_id']])->setInc('invite_upvaluation',0.02*$gf);
                        $three = [
                            'user_id'=>$two_refer_info['refer_id'],
                            'balance'=>0.02*$gf,
                            'type'=>6,
                            'add_time'=>date('Y-m-d H:i:s'),
                            'remarks'=>'作为投注人上级获利'
                        ];
        
                        M('upvaluation_exchange_records')->add($three);
                    }
                }
                M()->commit();
            }catch(\Exception $e){
                M()->rollback();
                showmessage($e->getMessage());
            }
            showmessage('操作成功',U('index'),1);
        }else{
            showmessage('非法访问');
        }
    }
    
    //玩法介绍
    public function introduce()
    {
        include template('/user/exchange/introduce');
    }

    //投注记录
    public function touzhu_records()
    {   
        $records  = M('upvaluation_put_records')->where(['user_id'=>$this->id])->order('id desc')->select();
        include template('/user/exchange/touzhu_records');
    }
}